<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖品名称" prop="prizeName">
        <el-input
          v-model="queryParams.prizeName"
          placeholder="请输入奖品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="兑换码" prop="prizeCode">
        <el-input
          v-model="queryParams.prizeCode"
          placeholder="请输入兑换码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否兑换" prop="isExchange">
        <el-select v-model="queryParams.isExchange" placeholder="请选择是否兑换" clearable>
          <el-option
            v-for="dict in dict.type.app_lottery_record_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="抽奖轮数" prop="lotteryIndex">
        <el-select v-model="queryParams.lotteryIndex" placeholder="请选择抽奖轮数" clearable>
          <el-option
            v-for="dict in 5"
            :key="dict"
            :label="'第' +  dict + '轮'"
            :value="dict"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>


    <el-table border stripe v-loading="loading" :data="lotteryRecordList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column width="80" show-overflow-tooltip label="编号" align="center" prop="id"/>
      <el-table-column label="轮数" width="80" align="center" prop="lotteryIndex"/>
      <el-table-column label="中奖时间" align="center" prop="createTime"/>
      <el-table-column label="部门名称" width="150" align="center" prop="deptName"/>
      <el-table-column label="用户姓名" width="150" align="center" prop="userName"/>
      <el-table-column label="手机号" align="center" prop="phonenumber"/>
      <el-table-column label="奖品名称" align="center" prop="prizeName"/>
      <el-table-column label="奖品图片" align="center" prop="prizeImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.prizeImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="奖品数量" width="100" align="center" prop="prizeNum"/>
      <el-table-column label="兑换码" width="180" align="center" prop="prizeCode"/>
      <el-table-column label="是否兑换" width="120" align="center" prop="isExchange">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_lottery_record_status" :value="scope.row.isExchange"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['applet:lotteryRecord:edit']"
          >确认
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分抽奖记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名"/>
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id"/>
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称"/>
        </el-form-item>
        <el-form-item label="奖品id" prop="prizeId">
          <el-input v-model="form.prizeId" placeholder="请输入奖品id"/>
        </el-form-item>
        <el-form-item label="奖品名称" prop="prizeName">
          <el-input v-model="form.prizeName" placeholder="请输入奖品名称"/>
        </el-form-item>
        <el-form-item label="奖品数量" prop="prizeNum">
          <el-input v-model="form.prizeNum" placeholder="请输入奖品数量"/>
        </el-form-item>
        <el-form-item label="奖品图片" prop="prizeImage">
          <image-upload v-model="form.prizeImage"/>
        </el-form-item>
        <el-form-item label="兑换码" prop="prizeCode">
          <el-input v-model="form.prizeCode" placeholder="请输入兑换码"/>
        </el-form-item>
        <el-form-item label="是否兑换" prop="isExchange">
          <el-select v-model="form.isExchange" placeholder="请选择是否兑换">
            <el-option
              v-for="dict in dict.type.app_lottery_record_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addLotteryRecord,
  delLotteryRecord,
  getLotteryRecord,
  listLotteryRecord,
  updateLotteryRecord
} from "@/api/background/lotteryRecord";

export default {
  name: "LotteryRecord",
  dicts: ['app_lottery_record_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 积分抽奖记录表格数据
      lotteryRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        deptId: null,
        deptName: null,
        prizeId: null,
        prizeName: null,
        prizeNum: null,
        prizeImage: null,
        prizeCode: null,
        isExchange: null,
        lotteryIndex: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        userName: [
          {required: true, message: "用户姓名不能为空", trigger: "blur"}
        ],
        deptId: [
          {required: true, message: "部门id不能为空", trigger: "blur"}
        ],
        deptName: [
          {required: true, message: "部门名称不能为空", trigger: "blur"}
        ],
        prizeId: [
          {required: true, message: "奖品id不能为空", trigger: "blur"}
        ],
        prizeName: [
          {required: true, message: "奖品名称不能为空", trigger: "blur"}
        ],
        prizeNum: [
          {required: true, message: "奖品数量不能为空", trigger: "blur"}
        ],
        prizeImage: [
          {required: true, message: "奖品图片不能为空", trigger: "blur"}
        ],
        prizeCode: [
          {required: true, message: "兑换码不能为空", trigger: "blur"}
        ],
        isExchange: [
          {required: true, message: "是否兑换不能为空", trigger: "change"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        createBy: [
          {required: true, message: "$comment不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询积分抽奖记录列表 */
    getList() {
      this.loading = true;
      listLotteryRecord(this.queryParams).then(response => {
        this.lotteryRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        deptId: null,
        deptName: null,
        prizeId: null,
        prizeName: null,
        prizeNum: null,
        prizeImage: null,
        prizeCode: null,
        isExchange: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分抽奖记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLotteryRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改积分抽奖记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLotteryRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLotteryRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分抽奖记录编号为"' + ids + '"的数据项？').then(function () {
        return delLotteryRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/lotteryRecord/export', {
        ...this.queryParams
      }, `积分抽奖中奖名单_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
